package com.java.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.java.entity.Doctor;
import com.java.utils.SqlCons;

/**
* @ClassName: DoctorDao
* @Description: 医生数据库操作
* @author ChenJiaLu
* @date 2023年12月29日 上午10:46:03
*
 */
public class DoctorDao extends BaseDao {

	public List<Doctor> getDoctors(){
		List<Doctor> list = new ArrayList<Doctor>();
		try {
			PreparedStatement statement = getConnection().prepareStatement(SqlCons.DOCTOR.SELECT_ALL);
			ResultSet res = statement.executeQuery();
			while(res.next()) {
				Doctor info = new Doctor();
				info.setId(res.getInt("id"));
				info.setNumber(res.getString("number"));
				info.setName(res.getString("name"));
				info.setAge(res.getInt("age"));
				info.setDegree(res.getString("degree"));
				info.setSex(res.getString("sex"));
				info.setPassword(res.getString("password"));
				list.add(info);
			}
		} catch (Exception e) {
			System.err.println(e.getMessage());
		} finally {
			close();
		}
		return list;
	}

	public void delDoctor(int id) {
		try {
			PreparedStatement statement = getConnection().prepareStatement(SqlCons.DOCTOR.DELETE);
			statement.setInt(1, id);
			statement.executeUpdate();
		} catch (Exception e) {
			System.err.println(e.getMessage());
		} finally {
			close();
		}
	}

	public void saveDoctor(Doctor doctor) {
		try {
			PreparedStatement statement = getConnection().prepareStatement(SqlCons.DOCTOR.INSERT);
			statement.setString(1, doctor.getNumber());
			statement.setString(2, doctor.getName());
			statement.setInt(3, doctor.getAge());
			statement.setString(4, doctor.getDegree());
			statement.setString(5, doctor.getSex());
			statement.setString(6, doctor.getPassword());
			statement.executeUpdate();
		} catch (Exception e) {
			System.err.println(e.getMessage());
		} finally {
			close();
		}
	}

	public void updateDoctor(Doctor doctor) {
		try {
			PreparedStatement statement = getConnection().prepareStatement(SqlCons.DOCTOR.UPDATE);
			statement.setString(1, doctor.getNumber());
			statement.setString(2, doctor.getName());
			statement.setInt(3, doctor.getAge());
			statement.setString(4, doctor.getDegree());
			statement.setString(5, doctor.getSex());
			statement.setString(6, doctor.getPassword());
			statement.setInt(7, doctor.getId());
			statement.executeUpdate();
		} catch (Exception e) {
			System.err.println(e.getMessage());
		} finally {
			close();
		}
	}
}
